package com.yonyou.isv.demo.openapi.base.datacenter.impl;

import com.yonyou.isv.demo.openapi.base.properties.OpenApiProperties;
import com.yonyou.isv.demo.openapi.base.datacenter.DataCenterUrlProvider;
import com.yonyou.isv.demo.openapi.base.datacenter.pojo.GatewayAddressResponse;
import com.yonyou.isv.demo.openapi.utils.RequestUtil;
import lombok.Getter;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * @author nishch
 * @description: TODO
 * @date 2022/3/8
 */
public abstract class DataCenterUrlProviderImpl implements DataCenterUrlProvider {

    @Resource
    @Getter
    RestTemplate restTemplate;
    @Resource
    OpenApiProperties properties;


    @Override
    public String buildBusinessUrl() {
        return queryGatewayAddress().getGatewayUrl();
    }

    @Override
    public String buildTokenUrl() {
        return queryGatewayAddress().getTokenUrl();
    }


    @Override
    @SneakyThrows
    public GatewayAddressResponse.GatewayAddressDTO queryGatewayAddress() {
        GatewayAddressResponse gatewayAddressResponse = RequestUtil.doGetType(properties.getGatewayAddressUrl().replace("%s",queryTenantId()),GatewayAddressResponse.class);
        return gatewayAddressResponse.getData();
    }


    @Override
    public String queryTenantId() {
        return properties.getTenantId();
    }
}
